/*
 * File:         TripTest.java
 * Author:       Robert Bittle <guywithnose@gmail.com>
 */
package triperest.tests;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import tests.AppEngineTest;
import triperest.Destination;
import triperest.Trip;

/**
 * The Class TripTest.
 */
public class TripTest extends AppEngineTest {

	/** The trip. */
	private Trip trip;
	
	/* (non-Javadoc)
	 * @see tests.AppEngineTest#setUp()
	 */
	@Before
	@Override
	public void setUp()
	{
		super.setUp();
		trip = new Trip();
		trip.setName("testName");
		trip.setDescription("This trip is fun.");
	}
	
	/* (non-Javadoc)
	 * @see tests.AppEngineTest#tearDown()
	 */
	@Override
	@After
	public void tearDown()
	{
		if(trip.getId() != 0)
			trip.delete();
		super.tearDown();
	}
	
	/**
	 * Tests the save method using insert.
	 */
	@Test
	public void testSave_Insert() {
		trip.save();
		Trip trip2 = new Trip(trip.getId());
		trip2.load();
		assertEquals(trip.getName(), trip2.getName());
		assertEquals(trip.getDescription(), trip2.getDescription());
	}
	
	/**
	 * Tests the save method using update.
	 */
	@Test
	public void testSave_Update() {
		trip.save();
		trip.setName("Another Test Name");
		trip.setDescription("Another Description");
		trip.save();
		Trip trip2 = new Trip(trip.getId());
		trip2.load();
		assertEquals(trip.getName(), trip2.getName());
		assertEquals(trip.getDescription(), trip2.getDescription());
	}
	
	/**
	 * Tests the delete method.
	 */
	@Test
	public void testDelete() {
		trip.save();
		assertTrue(trip.load());
		trip.delete();
		assertFalse(trip.load());
	}
	
	/**
	 * Tests the save method using insert destinations.
	 */
	@Test
	public void testSave_InsertDestinations() {
		Destination dest = new Destination();
		dest.setPlacesId("A test Place Id");
		dest.setName("My dest");
		trip.addDestination(dest);
		trip.save();
		Trip trip2 = new Trip(trip.getId());
		trip2.load();
		assertEquals(trip.getId(), trip2.getId());
		assertEquals(trip.getName(), trip2.getName());
		assertEquals(trip.getDestinations().get(0).getPlacesId(), trip2.getDestinations().get(0).getPlacesId());
		assertEquals(trip.getDestinations().get(0).getName(), trip2.getDestinations().get(0).getName());
		assertEquals(trip.getDestinations().get(0).getID(), trip2.getDestinations().get(0).getID());
		dest.delete();
	}
	
	/**
	 * Tests the save method using update destinations.
	 */
	@Test
	public void testSave_UpdateDestinations() {
		trip.save();
		Destination dest = new Destination();
		dest.setPlacesId("A test Place Id");
		dest.setName("My dest");
		trip.addDestination(dest);
		trip.save();
		trip.getDestinations().get(0).setPlacesId("abcdefg");
		trip.getDestinations().get(0).setName("ggffddssaa");
		trip.save();
		Trip trip2 = new Trip(trip.getId());
		trip2.load();
		assertEquals(trip.getId(), trip2.getId());
		assertEquals(trip.getName(), trip2.getName());
		assertEquals(trip.getDestinations().get(0).getPlacesId(), trip2.getDestinations().get(0).getPlacesId());
		assertEquals(trip.getDestinations().get(0).getName(), trip2.getDestinations().get(0).getName());
		assertEquals(trip.getDestinations().get(0).getID(), trip2.getDestinations().get(0).getID());
		dest.delete();
	}

}
